package com.grapecity.circlefriend.activity;

import java.util.ArrayList;

import com.grapecity.circlefriend.R;
import com.grapecity.control.gallery.CircleGallery;
import com.grapecity.control.util.DownloadImageTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

@SuppressWarnings("deprecation")
public class GalleryActivity extends Activity {

	private CircleGallery gallery;

	// tips
	private LinearLayout viewGroup = null;
	private ImageView[] imgViews = null;

	// items
	private String[] items;
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_gallery);
		gallery = (CircleGallery) findViewById(R.id.circleGallery);

		Intent intent = getIntent();
		@SuppressWarnings("unchecked")
		ArrayList<String> imgList = (ArrayList<String>) intent.getSerializableExtra("items");
		items = new String[imgList.size()];
		items = imgList.toArray(items);
		index = intent.getIntExtra("index", 0);

		GalleryImagesAdapter adapter = new GalleryImagesAdapter(this, items);
		gallery.setAdapter(adapter);
		gallery.setUnselectedAlpha(1f);
		gallery.setSelection(index);

		viewGroup = (LinearLayout) findViewById(R.id.viewGroup);
		imgViews = new ImageView[items.length];
		for (int i = 0; i < imgViews.length; i++) {
			imgViews[i] = new ImageView(this);

			if (i == index) {
				imgViews[i].setBackgroundResource(R.drawable.aqs);
			} else {
				imgViews[i].setBackgroundResource(R.drawable.akz);
			}
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new LayoutParams(8, 8));
			lp.leftMargin = 8;
			lp.rightMargin = 8;
			viewGroup.addView(imgViews[i], lp);
		}

		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				int pos = position % imgViews.length;
				for (int i = 0; i < imgViews.length; i++) {
					if (pos == i) {
						imgViews[i].setBackgroundResource(R.drawable.aqs);
					} else {
						imgViews[i].setBackgroundResource(R.drawable.akz);
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	}

	public class GalleryImagesAdapter extends BaseAdapter {

		private Context context;
		private String[] images;

		public GalleryImagesAdapter(Context context, String[] images) {
			this.context = context;
			this.images = images;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.images.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageView = new ImageView(context);

			imageView.setBackgroundColor(Color.rgb(0, 0, 0));
			// imageView.setAdjustViewBounds(true);
			imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			new DownloadImageTask(imageView).execute(this.images[position]);

			return imageView;
		}
	}
}
